package concurrent;

/**
 * 测试安全与不安全
 *
 * @author fengpeng
 * @date 2019-05-19  17:55
 */
public class MyThread extends Thread {


    private int count = 5;

    /**
     * 线程执行逻辑
     * synchronized 同步锁，其获取锁的顺序和代码顺序无关，竞争获取锁（即获取锁的顺序与CPU执行顺序一致）
     */
    @Override
    public synchronized void run() {
        count --;
        System.out.println(currentThread().getName()+"   count : "+count);
    }

    public static void main(String[] args){
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread,"t1");
        Thread t2 = new Thread(myThread,"t2");
        Thread t3 = new Thread(myThread,"t3");
        Thread t4 = new Thread(myThread,"t4");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}
